package com.eas.biz.service.statistic.impl;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eas.biz.dao.statistic.dataobject.QuestionAnswerStatisticDO;
import com.eas.biz.dao.statistic.dataobject.QuestionStatisticDO;
import com.eas.biz.dao.statistic.dataobject.QuestionStatisticQuery;
import com.eas.biz.dao.statistic.interfaces.QuestionAnswerStatisticDao;
import com.eas.biz.dao.statistic.interfaces.QuestionStatisticDao;
import com.eas.biz.service.statistic.domain.QuestionAnswerStatistic;
import com.eas.biz.service.statistic.domain.QuestionStatistic;
import com.eas.biz.service.statistic.interfaces.QuestionStatisticService;
import com.eas.biz.service.statistic.query.QuestionStatisticServiceQuery;

@Service("questionStatisticService")
public class QuestionStatisticServiceImpl implements QuestionStatisticService {

    @Autowired
    private QuestionStatisticDao questionStatisticDao;

    @Autowired
    private QuestionAnswerStatisticDao questionAnswerStatisticDao;

    public List<QuestionStatistic> getStatisticList(QuestionStatisticServiceQuery queryInput) {
        List<QuestionStatistic> list = new LinkedList<QuestionStatistic>();
        if (queryInput != null && queryInput.getExamId() > 0 && queryInput.getScopeId() > 0
                && StringUtils.isNotBlank(queryInput.getSubject())
                && StringUtils.isNotBlank(queryInput.getScopeType())) {
            List<QuestionStatisticDO> result = questionStatisticDao
                    .selectByQuery(buildQuery(queryInput));
            if (result != null) {
                for (QuestionStatisticDO qsDO : result) {
                    QuestionStatistic statistic = buildQuestionStatistic(qsDO,
                            queryInput.isWithAnswerStatistic());
                    list.add(statistic);
                }
            }
        }
        return list;
    }

    public List<QuestionAnswerStatistic> getAnswerStatisticList(long statisticId) {
        List<QuestionAnswerStatistic> list = new LinkedList<QuestionAnswerStatistic>();
        if (statisticId > 0) {
            QuestionStatisticQuery query = new QuestionStatisticQuery();
            query.setStatisticId(statisticId);
            // query.orderByAnswer(false);
            List<QuestionAnswerStatisticDO> result = questionAnswerStatisticDao
                    .selectByQuery(query);
            if (result != null) {
                for (QuestionAnswerStatisticDO qasDO : result) {
                    QuestionAnswerStatistic statistic = buildQuestionAnswerStatistic(qasDO);
                    list.add(statistic);
                }
            }
        }
        return list;
    }

    private QuestionAnswerStatistic buildQuestionAnswerStatistic(QuestionAnswerStatisticDO qasDO) {
        QuestionAnswerStatistic statistic = new QuestionAnswerStatistic();
        statistic.setStatisticId(qasDO.getStatisticId());
        statistic.setAnswer(qasDO.getAnswer());
        statistic.setCount(qasDO.getCount());
        return statistic;
    }

    private QuestionStatistic buildQuestionStatistic(QuestionStatisticDO qsDO,
            boolean withAnswerStatistic) {
        QuestionStatistic statistic = new QuestionStatistic();
        statistic.setExamId(qsDO.getExamId());
        statistic.setScopeId(qsDO.getScopeId());
        statistic.setScopeType(qsDO.getScopeType());
        statistic.setSubject(qsDO.getSubject());
        statistic.setObjective(qsDO.getIsObjective() != null && 1 == qsDO.getIsObjective());
        statistic.setQuestionNumber(qsDO.getQuestionNumber());
        statistic.setTotalScore(qsDO.getTotalScore());
        statistic.setMaxScore(qsDO.getMaxScore());
        statistic.setMinScore(qsDO.getMinScore());
        statistic.setAvgScore(qsDO.getAvgScore());
        statistic.setDiviation(qsDO.getDiviation());
        statistic.setTotalCount(qsDO.getTotalCount());
        statistic.setCorrectCount(qsDO.getCorrectCount());
        statistic.setHighScoreCount(qsDO.getHighScoreCount());

        if (statistic.isObjective() && withAnswerStatistic) {
            statistic.setAnswerStatisticList(getAnswerStatisticList(qsDO.getId()));
            statistic.buildAnswerMap();
        }
        return statistic;
    }

    private QuestionStatisticQuery buildQuery(QuestionStatisticServiceQuery queryInput) {
        QuestionStatisticQuery query = new QuestionStatisticQuery();
        query.setExamId(queryInput.getExamId());
        query.setSubject(queryInput.getSubject());
        query.setScopeId(queryInput.getScopeId());
        query.setScopeType(queryInput.getScopeType());
        query.setIsObjective(queryInput.isObjective());
        return query;
    }
}
